package navigation.wzg.com.navigation.activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import navigation.wzg.com.navigation.R;
import navigation.wzg.com.navigation.Soaked.Soaked;
import navigation.wzg.com.navigation.Tools.Toolstotal;
import navigation.wzg.com.navigation.http.UpdateGET;

public class UpdateApp extends AppCompatActivity {

    @BindView(R.id.uu)
    LinearLayout uu;
    @BindView(R.id.update_toolbar)
    Toolbar updateToolbar;
    @BindView(R.id.v_name)
    TextView vName;
    @BindView(R.id.h_name)
    TextView hName;
    @BindView(R.id.yes_update)
    Button yesUpdate;
    @BindView(R.id.no_update)
    Button noUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_app);
        ButterKnife.bind(this);
        Soaked.initState(this, uu);
        updateToolbar.setTitle("检查更新");
        setSupportActionBar(updateToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        updateToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                overridePendingTransition(R.anim.out, R.anim.up);
            }
        });
        update();
        nname();
    }

    @OnClick({R.id.yes_update, R.id.no_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.yes_update:
                Intent intent= new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://119.29.203.29/app-debug.apk");
                intent.setData(content_url);
                startActivity(intent);
                break;
            case R.id.no_update:
                break;
        }
    }
    public void update(){
        String vNames = Toolstotal.packageName(UpdateApp.this);
        vName.setText(vNames);
    }

    public void nname(){
        UpdateGET.upadteget("http://119.29.203.29/update.html",new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                try {
                    JSONObject json = new JSONObject(msg.obj.toString());
                    String aa = json.getString("versions");
                    hName.setText(aa);
                    Contrast();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void Contrast(){
            if (hName.getText().toString().equals(vName.getText().toString())){
                yesUpdate.setVisibility(View.GONE);
                noUpdate.setVisibility(View.VISIBLE);
            }else {
                yesUpdate.setVisibility(View.VISIBLE);
                noUpdate.setVisibility(View.GONE);
            }
        }
}
